package com.project.nimsen.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.project.nimsen.dao.IBaseDao;
import com.project.nimsen.dto.AuthDto;
import com.project.nimsen.entity.Authority;
import com.project.nimsen.service.IAuthorityService;
import com.project.nimsen.util.NimsenUtil;
import com.project.jdbc.dao.IBaseJdbcDao;
import com.project.jdbc.service.BaseServiceImpl;
import com.project.jdbc.utils.Finder;
@Repository("authorityService")
public class AuthorityServiceImpl extends BaseServiceImpl implements IAuthorityService {
	@Resource
	IBaseDao baseDao;
	protected IBaseJdbcDao getBaseDao() {
		return baseDao;
	}
	
	public List<Map<String, Object>> findAuthorityByUserId(String userId) throws Exception {
		Finder finder = new Finder("select * from authority where active!=1 and ((id in	(select"
			+ " authorid from userauthor where active!=1 and userid=:userid1)) or (id in (select"
			+ " authorid from dutyauthor where active!=1 and dutyid in (select dutyid from userduty"
			+ " where active!=1 and userid=:userid2))) or (id in (select authorid from roleauthor where"
			+ " active!=1 and roleid in (select roleid from userole where active!=1 and userid="
			+ ":userid3))))");
		finder.setParam("userid1", userId);
		finder.setParam("userid2", userId);
		finder.setParam("userid3", userId);
		finder.append(" order by createTime asc");
		return baseDao.queryForList(finder);
	}
	public List<Map<String, Object>> findAllAuthority() throws Exception {
		Finder finder = new Finder("select * from authority where active!=1");
		finder.append(" order by createTime asc");
		return baseDao.queryForList(finder);
	}

	public List<Map<String, Object>> findByDto(AuthDto dto) throws Exception {
		Finder finder = new Finder("select au.id, au.authname, au.authurl, au.number, au.pnumber, "
			+ "au.createTime, au.active, us.username as createUserName from authority as au left "
			+ "join user as us on us.id = au.createUser where au.active!=1 and au.active!=1");
		if(StringUtils.isNotBlank(dto.getId())){
			finder.append(" and au.pnumber in (select number from authority where active!=1 and id=:dtoid)");
			finder.setParam("dtoid", dto.getId());
		}
		finder.append(" order by au.createTime asc");
		return baseDao.queryForList(finder);
	}

	public String findNextNum(String pnumber) throws Exception {
		Finder finder = new Finder("select count(id) from authority where 1=1");
		if(StringUtils.isNotBlank(pnumber)){
			finder.append(" and pnumber=:pnumber");
			finder.setParam("pnumber", pnumber);
		}else
			finder.append(" and (pnumber is null or pnumber='' or pnumber='0')");
		Map<String, Object> map = baseDao.queryForObject(finder);
		Integer count = Integer.parseInt(map.get("count(id)").toString());
		count++;
		if(StringUtils.isNotBlank(pnumber))
			return pnumber+"-"+count;
		else
			return count+"";
		/*if(count>999)
			return null;
		if(StringUtils.isNotBlank(pnumber))
			return pnumber+NimsenUtil.threeDigit(count);
		return NimsenUtil.threeDigit(count);*/
	}

	public Map<String, Object> findVilidate(AuthDto dto) throws Exception {
		if(dto==null)
			return null;
		Finder finder = new Finder("select id from authority where active!=1");
		finder.append(" and ").append(dto.getValifield()).append("=:filed");
		finder.setParam("filed", dto.getValivalue());
		if(StringUtils.isNotBlank(dto.getPnumber())){
			finder.append(" and pnumber=:pnumber");
			finder.setParam("pnumber", dto.getPnumber());
		}else
			finder.append(" and (pnumber is null or pnumber='' or pnumber='0')");
		//如果id存在表示为修改
		if(StringUtils.isNotBlank(dto.getId())){
			finder.append(" and id!=:id");
			finder.setParam("id", dto.getId());
		}
		return baseDao.queryForObject(finder);
	}

	public Authority findEntityById(String id) throws Exception {
		if(StringUtils.isBlank(id))
			return null;
		Finder finder = new Finder("select id,authname,authurl,number,pnumber,"
			+ "createUser,createTime,active from authority where active!=1 and "
			+ "id=:id");
		finder.setParam("id", id);
		finder.append(" order by createTime asc");
		return baseDao.queryForObject(finder, Authority.class);
	}

	public Map<String, Object> findEntity(AuthDto dto) throws Exception {
		if(dto==null)
			return null;
		Finder finder = new Finder("select id,authname,authurl,number,pnumber,"
				+ "createUser,createTime,active from authority where active!=1");
		if(StringUtils.isNotBlank(dto.getId())){
			finder.append(" and id=:id");
			finder.setParam("id", dto.getId());
		}
		finder.append(" order by createTime asc");
		return baseDao.queryForObject(finder);
	}

	public boolean deleteByDto(AuthDto dto) throws Exception {
		Finder finder = new Finder("update authority set active=1 where active!=1");
		if(StringUtils.isNotBlank(dto.getId())){
			finder.append(" and id=:id");
			finder.setParam("id", dto.getId());
		}
		return baseDao.update(finder)>0;
	}
}
